﻿/// <reference path="libs/_references.js" />
/// <reference path="../userClient/app/views.js" />
/// <reference path="../userClient/app/view-models.js" />


(function () {

    /* Routes */

    var appLayout =
        new kendo.Layout('<div id="main-content"></div>');
    var dataPersister = persisters.getPersister("/api/");
    
    var router = new kendo.Router();
    router.route("/home", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").addClass("visible");
        }
        viewsFactory.getHomeView() // getting html template here
            .then(function (homeViewHtml) {
                var view = new kendo.View(homeViewHtml);
                appLayout.showIn("#main-content", view);
            }, function (err) {
                var errorMsg = $("<div />").html(err.JSON.response.message);
                $("#main-content").append(errorMsg);
            });
    });

    router.route("/login", function () {
        if (dataPersister.isUserLoggedIn()) {
            router.navigate("/todo-list");
            $("#btn-logout").removeClass("hidden");
        } else {
            viewsFactory.getLoginView() // getting html template here
                .then(function (loginViewHtml) {
                    var loginViewModel = viewModelsFactory.getLoginViewModel(function () {
                        router.navigate("/todo-list");
                        $("#btn-logout").removeClass("hidden");
                    }); //data model
                    $("#main-content").empty();
                    var view = new kendo.View(loginViewHtml, { model: loginViewModel }); //view 
                    appLayout.showIn("#main-content", view);
                });
        }
    });

    router.route("/register", function () {
        if (dataPersister.isUserLoggedIn()) {
            router.navigate("/todo-list");
            $("#btn-logout").removeClass("hidden");
        } else {
            viewsFactory.getRegisterView() // getting html template here
                .then(function (registerViewHtml) {
                    var registerViewModel = viewModelsFactory.getRegisterViewModel(function () {
                        alert("Successful registration, click OK to continue with LOGIN");
                        router.navigate("/login");
                    }); //data model
                    var view = new kendo.View(registerViewHtml, { model: registerViewModel }); //view 
                    appLayout.showIn("#main-content", view);
                });
        }
    });

    router.route("/logout", function () {
        dataPersister.users.logout().then(function () {
            $("#main-content").empty();
            $("#btn-logout").addClass("hidden");
            router.navigate("/home");
        });
    });

    router.route("/todo-list/create", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.createTodoListView()
            .then(function (todoCreateViewHtml) {
                var createTodoViewModel = viewModelsFactory.createTodoListViewModel();//data model
                $("#main-content").empty();
                var view = new kendo.View(todoCreateViewHtml, { model: createTodoViewModel }); //view 
                appLayout.showIn("#main-content", view);
            });
    });
    
    router.route("/todo-list/:id/create", function (id) {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.createSingleTodoView()
            .then(function (signleTodoHtmlView) {
                var singleTodoViewModel = viewModelsFactory.createSingleToDoViewModel(id);//data model
                $("#main-content").empty();
                var view = new kendo.View(signleTodoHtmlView, { model: singleTodoViewModel }); //view 
                appLayout.showIn("#main-content", view);
            });
    });

    router.route("/todo-list", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.getTodosView()
            .then(function (todosViewHtml) {
                viewModelsFactory.getTodosViewModel()
                    .then(function (todosViewModel) {
                        var view = new kendo.View(todosViewHtml, { model: todosViewModel });
                        $("#main-content").empty();
                        appLayout.showIn("#main-content", view);
                    }, function (err) {
                        console.log(err);
                    });
            });
    });

    router.route("/todo-list/:id", function (id) {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.getSelectedTodoView()
            .then(function (todoViewHtml) {
                viewModelsFactory.getTodosByIdViewModel(id)
                    .then(function (todoViewModel) {
                        var view = new kendo.View(todoViewHtml, { model: todoViewModel });
                        $("#main-content").empty();
                        appLayout.showIn("#main-content", view);
                    }, function (err) {
                        console.log(err);
                    });
            });
    });

    router.route("/appointments/all", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.getAppointmentsView()
            .then(function (appointmentViewHtml) {
                viewModelsFactory.getAppointmentsViewModel()
                    .then(function (appointmentViewModel) {
                        var view = new kendo.View(appointmentViewHtml, { model: appointmentViewModel });
                        $("#main-content").empty();
                        appLayout.showIn("#main-content", view);
                    }, function (err) {
                        console.log(err);
                    });
            });
    });

    router.route("/appointments/upcomming", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.getAppointmentsView()
            .then(function (appointmentViewHtml) {
                viewModelsFactory.getCommingAppointmentsViewModel()
                    .then(function (appointmentViewModel) {
                        var view = new kendo.View(appointmentViewHtml, { model: appointmentViewModel });
                        $("#main-content").empty();
                        appLayout.showIn("#main-content", view);
                    }, function (err) {
                        console.log(err);
                    });
            });
    });

    router.route("/appointments/today", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.getAppointmentsView()
            .then(function (appointmentViewHtml) {
                viewModelsFactory.getTodaysAppointmentsViewModel()
                    .then(function (appointmentViewModel) {
                        var view = new kendo.View(appointmentViewHtml, { model: appointmentViewModel });
                        $("#main-content").empty();
                        appLayout.showIn("#main-content", view);
                    }, function (err) {
                        console.log(err);
                    });
            });
    });

    router.route("/appointments/current", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.getAppointmentsView()
            .then(function (appointmentViewHtml) {
                viewModelsFactory.getCurrentAppointmentsViewModel()
                    .then(function (appointmentViewModel) {
                        var view = new kendo.View(appointmentViewHtml, { model: appointmentViewModel });
                        $("#main-content").empty();
                        appLayout.showIn("#main-content", view);
                    }, function (err) {
                        console.log(err);
                    });
            });
    });
    
    router.route("/appointments/byDate", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        var date = $("#tb-app-date").val();
        if (date) {
            viewsFactory.getAppointmentsView()
                .then(function(appointmentViewHtml) {
                    viewModelsFactory.getAppointmetsByDateViewModel(date)
                        .then(function(appointmentViewModel) {
                            var view = new kendo.View(appointmentViewHtml, { model: appointmentViewModel });
                            $("#main-content").empty();
                            appLayout.showIn("#main-content", view);
                        }, function(err) {
                            console.log(err);
                        });
                });
        } else {
            alert("Specify date (Sorry, no time for more usability)");
            router.navigate("/appointments/all");
        }
        
    });

    router.route("/appointments/create", function () {
        if (dataPersister.isUserLoggedIn()) {
            $("#btn-logout").removeClass("hidden");
        }
        viewsFactory.createAppointmentView()
            .then(function (appointmentCreateViewHtml) {
                var createAppointmentViewModel = viewModelsFactory.createAppointmentViewModel();//data model
                $("#main-content").empty();
                var view = new kendo.View(appointmentCreateViewHtml, { model: createAppointmentViewModel }); //view 
                appLayout.showIn("#main-content", view);
            });
    });

    $(function () {
        appLayout.render("#app");
        router.start("/home");
    });
}());

